package com.fly.toolbox.component;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationGroupManager;
import com.intellij.notification.Notifications;
import com.intellij.openapi.ui.MessageType;

import java.util.Timer;
import java.util.TimerTask;

public class AppNotifier {

    public static final  String groupId = "Toolbox Notification Group";

    public static void notifyInfo(String content) {
        notify(content, MessageType.INFO);
    }

    public static void notifyError(String content) {
        notify(content, MessageType.ERROR);
    }

    private static void notify(String content, MessageType messageType) {
        NotificationGroupManager groupManager = NotificationGroupManager.getInstance();
        Notification notification = groupManager.getNotificationGroup(groupId)
                .createNotification(content, messageType);
        Notifications.Bus.notify(notification);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                notification.expire();
                timer.cancel(); // 取消计时器
            }
        }, 2000);
    }
}
